package com.minshenglife.zanzan.service.power.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.minshenglife.commons.dal.Page;
import com.minshenglife.zanzan.dao.power.UserRoleDao;
import com.minshenglife.zanzan.entity.Dept;
import com.minshenglife.zanzan.entity.power.UserDeptRole;
import com.minshenglife.zanzan.pub.CalcUtils;
import com.minshenglife.zanzan.service.power.UserRoleService;

/**
 * 用户角色/职位
 * 
 * @author wangpeng
 * 
 */
@Component
@Transactional
public class UserRoleServiceImpl implements UserRoleService {
	Logger log = LoggerFactory.getLogger(UserPowerServiceImpl.class);
	@Resource
	private UserRoleDao userroleDao;

	@Override
	public Page<UserDeptRole> findUserdept(Map<String, Object> map, int pageNo,
			int pageSize) {
		Page<UserDeptRole> ret = new Page<UserDeptRole>();
		int offset = (pageNo - 1) * pageSize;
		Long count = userroleDao.countUser();
		map.put("from", offset);
		map.put("to", offset + pageSize);
		List<UserDeptRole> list = userroleDao.findAllUser(map);
		ret.setPageNo(pageNo);
		ret.setPageSize(pageSize);
		ret.setRows(list);
		ret.setTotalPage(CalcUtils.calcTotalPage(pageSize, count));
		ret.setTotalRow(count);
		return ret;
	}

	@Override
	public Page<UserDeptRole> findUserName(Map<String, Object> map,
			String userName) {
		Page<UserDeptRole> ret = new Page<UserDeptRole>();
		map.put("userName", userName);
		List<UserDeptRole> list = userroleDao.findUserName(map);
		ret.setRows(list);
		return ret;
	}

	@Override
	public UserDeptRole findUserDeptRole(Map<String, Object> map, String empId) {
		map.put("empId", empId);
		return userroleDao.findUserDept(map);
	}

	@Override
	public String findDept(String dept_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dept_id", dept_id);
		List<Dept> list = userroleDao.findDeptName(map);
		StringBuffer sb = new StringBuffer(
				"{\"msg\":[{\"tatle\":\"成功\",\"parent\":\"" + dept_id
						+ "\"}],\"data\":[");
		if (null != list) {
			for (int i = 0; i < list.size(); i++) {
				if (i != 0) {
					sb.append(",");
				}
				sb.append("{\"dept_id\":\"" + list.get(i).getDept_id() + "\",");
				sb.append("\"abbr_name\":\"" + list.get(i).getAbbr_name()
						+ "\",");
				sb.append("\"dept_name\":\"" + list.get(i).getDept_name()
						+ "\",");
				sb.append("\"dept_level\":\"" + list.get(i).getDept_level()
						+ "\"}");
			}
		}
		sb.append("]}");
		return sb.toString();
	}

	@Override
	public String findfFiliale(String dept_id) {
		Dept dept = userroleDao.findDeptId(dept_id);
		StringBuffer sb = new StringBuffer(
				"{\"msg\":[{\"tatle\":\"成功\",\"parent\":\"" + dept_id
						+ "\"}],\"data\":[");
		if (null != dept) {
			sb.append("{\"dept_id\":\"" + dept.getDept_id() + "\",");
			sb.append("\"abbr_name\":\"" + dept.getAbbr_name() + "\",");
			sb.append("\"dept_name\":\"" + dept.getDept_name() + "\",");
			sb.append("\"dept_level\":\"" + dept.getDept_level() + "\"}");
			sb.append("]}");
		}
		return sb.toString();
	}

	@Override
	public Dept getDept(String dept_id) {
		return userroleDao.findDeptId(dept_id);
	}
	
}
